﻿using App.Dal;
using App.Model;
using App.Untiys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Win.Dal
{
    public class SysUserServices
    {
        static SqlSugarScope dbHelper = SqliteSugarHelper.Db;

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static SysUser Info(SysUser model)
        {
            return dbHelper.Queryable<SysUser>()
                .LeftJoin<SysRole>((_user, _role) => _user.RoleId == _role.Id)
                .LeftJoin<ProDepart>((_user, _role, _depart) => _user.DepartId == _depart.Id)
                .WhereIF(!string.IsNullOrEmpty(model.Acct), _user => _user.Acct == model.Acct && _user.IsDelete == 0 && _user.Disable == 0)
                .WhereIF(model.Id > 0, _user => _user.Id == model.Id)
                .Select((_user, _role, _depart) => new SysUser
                {
                    Acct = _user.Acct,
                    Address = _user.Address,
                    AddTime = _user.AddTime,
                    AddUser = _user.AddUser,
                    ChName = _user.ChName,
                    DepartId = _user.DepartId,
                    Disable = _user.Disable,
                    EmergencyContact = _user.EmergencyContact,
                    EmergencyPhone = _user.EmergencyContact,
                    Id = _user.Id,
                    IsAdmin = _user.IsAdmin,
                    IsDelete = _user.IsDelete,
                    Phone = _user.Phone,
                    Pwd = _user.Pwd,
                    RoleId = _user.RoleId,
                    RoleName = _role.RoleName,
                    DepartName = _depart.DepartName,
                    Authority = _role.Authority,
                    Abandon = _user.Abandon
                })
                .First();
        }

        /// <summary>
        /// 判断名称是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static bool Isrepeat(int id, string Name = "")
        {
            if (id <= 0)
                return dbHelper.Queryable<SysUser>().Where(m => m.Acct == Name).Any();
            else
                return dbHelper.Queryable<SysUser>().Where(m => m.Acct == Name && m.Id != id).Any();
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<SysUser> GetUseList(SysUser model)
        {
            return dbHelper.Queryable<SysUser>().ToList();
        }

        /// <summary>
        /// 获取列表 分页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<SysUser> GetPageList(QueryPageModel model, ref int totalCount)
        {
            model.Order = model.Order.Contains("==") ? "" : model.Order;
            DateTime StartTime = model.StartTime.ToDateTimeEx(DateTime.Now);
            DateTime EndTime = model.EndTime.ToDateTimeEx(DateTime.Now);
            var sugarObj = dbHelper.Queryable<SysUser, ProDepart>((_user, _depart) => new JoinQueryInfos(
                                JoinType.Left, _user.DepartId == _depart.Id
                            ))
                .Where((_user, _depart) => _user.IsAdmin == 0)
                .Where((_user, _depart) => _user.IsDelete == 0)
                .WhereIF(!string.IsNullOrEmpty(model.KeyWord), (_user, _depart) => _user.ChName.Contains(model.KeyWord) || _user.Phone.Contains(model.KeyWord) || _user.EmergencyPhone.Contains(model.KeyWord))
                .WhereIF(!string.IsNullOrEmpty(model.StartTime), (_user, _depart) => _user.AddTime >= StartTime)
                .WhereIF(!string.IsNullOrEmpty(model.EndTime), (_user, _depart) => _user.AddTime <= EndTime);

            string sql = sugarObj.ToSqlString();
            if (model.Order == "时间正序")
                sugarObj = sugarObj.OrderBy((_user, _depart) => _user.AddTime, OrderByType.Asc);
            else if (model.Order == "时间倒序")
                sugarObj = sugarObj.OrderBy((_user, _depart) => _user.AddTime, OrderByType.Desc);
            else
                sugarObj = sugarObj.OrderBy((_user, _depart) => _user.AddTime, OrderByType.Desc);
            return sugarObj.Select((_user, _depart) => new SysUser
            {
                Acct = _user.Acct,
                Address = _user.Address,
                AddTime = _user.AddTime,
                AddUser = _user.AddUser,
                ChName = _user.ChName,
                DepartId = _user.DepartId,
                Disable = _user.Disable,
                EmergencyContact = _user.EmergencyContact,
                EmergencyPhone = _user.EmergencyPhone,
                Id = _user.Id,
                IsAdmin = _user.IsAdmin,
                IsDelete = _user.IsDelete,
                Phone = _user.Phone,
                Pwd = _user.Pwd,
                DepartName = _depart.DepartName,
                ShowTime = _user.AddTime.ToString("yyyy-MM-dd HH:mm"),
            }).ToPageList(model.PageIndex, model.PageSize, ref totalCount);
        }

        /// <summary>
        /// 新增/编辑用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Edit(SysUser model)
        {
            model.AddTime = DateTime.Now;
            model.IsDelete = 0;
            if (model.Id <= 0)
                return dbHelper.Insertable<SysUser>(model).ExecuteCommand() > 0;
            return dbHelper.Updateable<SysUser>(model).IgnoreColumns(m => new
            {
                m.AddTime,
                m.IsDelete,
                m.Id
            }).Where(m => m.Id == model.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 新增/编辑用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool ChangePassword(SysUser model)
        {
            return dbHelper.Updateable<SysUser>(model).UpdateColumns(m => new
            {
                m.Pwd
            }).Where(m => m.Id == model.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            return dbHelper.Updateable<SysUser>()
                        .SetColumns(it => new SysUser() { IsDelete = 1 })//类只能在表达示里面不能提取
                        .Where(it => it.Id == id)
                        .ExecuteCommand() > 0;
        }
    }
}
